package scau.mis4.chanyulin.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.entity.Good;
import scau.mis4.chanyulin.entity.GoodDetail;
import scau.mis4.chanyulin.entity.Stock;
import scau.mis4.chanyulin.service.IGoodDetailService;
import scau.mis4.chanyulin.service.IGoodService;
import scau.mis4.chanyulin.service.IStockService;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * Good Controller层
 */
@RestController
@RequestMapping("/good")
public class GoodController extends BaseController {

    @Autowired
    private IGoodService goodService;
    @Autowired
    private IGoodDetailService goodDetailService;
    @Autowired
    private IStockService stockService;

    /**
     * 添加Good
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
//    @ResponseBody
    @RequiresRoles(value={"0","1", "3"},logical = Logical.OR)
    public String addGood(@RequestBody JSONObject jsonObject) throws Exception {
        Good good = jsonObject.getObject("good", Good.class);

        Stock stock = jsonObject.getObject("stock", Stock.class);

        JSONArray goodDetailArrays = jsonObject.getJSONArray("goodDetailList");
        List<GoodDetail> goodDetailList = JSON.parseArray(goodDetailArrays.toString(), GoodDetail.class);

        goodService.add(good);
        stockService.add(stock);
        for (GoodDetail goodDetail : goodDetailList) {
            goodDetail.setStockId(stock.getId());
            goodDetail.setGoodId(good.getId());
            goodDetailService.add(goodDetail);
        }

        return createSuccessJSON();
    }

//    /**
//     * 添加Good
//     * @return
//     * @throws Exception
//     */
//    @PostMapping("/add")
//    @ResponseBody
//    public String addGood(Good good, List<GoodDetail> goodDetailList, Stock stock) throws Exception {
//        goodService.add(good);
//        stockService.add(stock);
//        for (GoodDetail goodDetail : goodDetailList) {
//            goodDetail.setStockId(stock.getId());
//            goodDetail.setGoodId(good.getId());
//            goodDetailService.add(goodDetail);
//        }
//        return createSuccessJSON();
//    }

    /**
     * 删除所有Good和GoodDetail
     * @param goodId
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"0","1", "3"},logical = Logical.OR)
    @PostMapping("/delete_all")
    public String deleteGoodAndDetailById(String goodId) throws Exception {
        return createJSONByUpdates(goodService.deleteGoodAndDetail(goodId));
    }

    /**
     * 查找某个Good
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get_by_id")
    public String getGoodById(String id) throws Exception {
        Good good = goodService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", good);
        return createSuccessJSON(model);
    }

    /**
     * 通过good_no查找某个Good
     * @param goodNo
     * @return
     * @throws Exception
     */
    @GetMapping("/get_by_goodno")
    public String getGoodByGoodNo(String goodNo) throws Exception {
        Good good = goodService.findByGoodNo(goodNo);
        Model model = new ExtendedModelMap().addAttribute("result", good);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部Good
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllGood() throws Exception {
        List<Good> goodList = goodService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result", goodList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询Good
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listGoodByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Good> goodList = goodService.findAll();
        PageInfo<Good> pageInfo = new PageInfo<>(goodList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", goodList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个Good
     * @param good
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @RequiresRoles(value={"0","1", "3"},logical = Logical.OR)
    public String updateGoodById(Good good) throws Exception {
        goodService.updateById(good);
        return createSuccessJSON();
    }

}
